from collections import OrderedDict

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


def meiduo_jwt_response_payload_handler(token, user=None, request=None):


    return {
        'token': token,
        'username': user.username,
        'id': user.id
    }


# PageNumberPagination
# 自定义分页类
class PageNum(PageNumberPagination):
    page_size = 2  # 默认每页返回的条数
    page_size_query_param = 'pagesize'  # url中设置 page_size的键,默认为page_size
    max_page_size = 20  # 每页返回的最大条数

    # 重写分页返回数据的方法
    # 修该返回数据的格式
    def get_paginated_response(self, data):
        print(self.page.paginator.num_pages)  # 1 总页数
        print(self.page.paginator.per_page)  # 2 每页数量
        print(self.page.number)  # 3 第几页

        return Response(OrderedDict([
            ('pages', self.page.paginator.num_pages),
            ('page', self.page.number),
            ('lists', data)
        ]))

